package aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @Description
 * @Author 李楚鹏  ，
 * @Date 2024/12/13 9:35
 **/

public class TransactionAspect {
    private static final Logger log= LoggerFactory.getLogger(TransactionAspect.class);

    @Before(value = "execution(* service..*.*(..))")
    public void pt(){}
    @Before(value = "pt()")
    public void before(JoinPoint jp){
        log.info("【TransactionAspect 前置增强】模拟打开数据库连接并开启事务");
        log.info("SqlSession sqlSession=MyBatisUtil.createSqlSession(false;");
    }
    @AfterReturning(value = "pt()",returning = "result")
    public void afterReturning(JoinPoint jp,Object result){
        log.info("【TransactionAspect 后置增强】模拟正常完成业务提交事务...");
        log.info("session.commit()");
    }
    @AfterThrowing(value = "pt()",throwing = "e")
    public void afterThrowing(JoinPoint jp,Throwable e){
        log.info("【TransactionAspect 异常抛出增强】模拟业务回滚事务，异常信息为："+e.getMessage());
        log.info("session.rollback()");
    }
    @After(value = "pt()")
    public void after(JoinPoint jp){
        log.info("【TransactionAspect 最终增强】模拟业务完成释放资源...");
        log.info("MyBatisUtil.closeSqlSession(SqlSession)");
    }
}